package com.api.shopapi.controller;


import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.ActivityItemDTO;
import com.api.shopapi.service.ActivityItemService;
import com.api.shopapi.service.ActivityTypeService;
import com.api.shopapi.service.CommandCenterStaffRelService;
import com.api.shopapi.service.StaffService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 活动项目
 * */

@RestController
@RequestMapping("ActivityItem")
public class ActivityItemController extends BaseController{


    @Autowired
    @Qualifier("ActivityItemServiceImplOne")
    private ActivityItemService activityItemService;

    @Autowired
    @Qualifier("CommandCenterStaffRelServiceImplOne")
    private CommandCenterStaffRelService commandCenterStaffRelService;


    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;


    @Autowired
    @Qualifier("ActivityTypeServiceImplOne")
    private ActivityTypeService activityTypeService;


    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String name = jsonObject.getString("name");
            String activityTypeCode = jsonObject.getString("activityTypeCode");

            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityItem/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);

            Staff staff = staffService.getByStaffCode(staffCode);

            ActivityItem activityItem =new ActivityItem();
            activityItem.setActivityItemCode(UUID.randomUUID().toString());
            activityItem.setActivityTypeCode(activityTypeCode);
            activityItem.setName(name);
            activityItem.setStatus(1);
            activityItem.setCreateTime(new Date());
            activityItem.setGovernmentCode(staff.getGovernmentCode());
            activityItem.setCommandCenterCode(commandCenterStaffRel.getCommandCenterCode());
            activityItemService.insert(activityItem);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityItem/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String name = jsonObject.getString("name");
            String activityTypeCode = jsonObject.getString("activityTypeCode");
            int status = jsonObject.getInteger("status");

            String staffCode = jsonObject.getString("staffCode");

            String activityItemCode = jsonObject.getString("activityItemCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityItem/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ActivityItem activityItem = activityItemService.getByActivityItemCode(activityItemCode);
            activityItem.setName(name);
            activityItem.setStatus(status);
            activityItem.setActivityTypeCode(activityTypeCode);
            activityItemService.update(activityItem);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityItem/Edit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult getList(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String activityTypeCode = jsonObject.getString("activityTypeCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityItem/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            List<ActivityItem> list = activityItemService.ListByActivityTypeCode(activityTypeCode);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("list",list);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityItem/List",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            String activityTypeCode = jsonObject.getString("activityTypeCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ActivityItem/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            CommandCenterStaffRel commandCenterStaffRel = commandCenterStaffRelService.getByStaffCode(staffCode);

            PageInfo<ActivityItem> pageInfo = activityItemService.pageByCommandCenterCodeActivityTypeCode(pageIndex,pageSize,
                    commandCenterStaffRel.getCommandCenterCode(),activityTypeCode);

            List<ActivityItemDTO> listDTO = new ArrayList<>();
            for(ActivityItem activityItem:pageInfo.getList()){
                ActivityItemDTO activityItemDTO = mapper.map(activityItem,ActivityItemDTO.class);

                ActivityType activityType = activityTypeService.getByActivityTypeCode(activityItem.getActivityTypeCode());
                activityItemDTO.setActivityTypeName(activityType.getName());

                listDTO.add(activityItemDTO);
            }


            PageInfo<ActivityItemDTO> pageInfo1 = new PageInfo<>();
            pageInfo1.setList(listDTO);
            pageInfo1.setTotal(pageInfo.getTotal());

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo1);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/ActivityItem/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }



}
